<template>
  <div>
    <div>
      <div>
        <el-form :inline="true" :model="searchData" class="demo-form-inline">
          <el-form-item label="球场名称：">
            <el-input
              v-model="searchData.siteName"
              placeholder="选择球场名称"
            ></el-input>
          </el-form-item>
          <el-form-item label="起始日期：">
            <el-date-picker
              v-model="searchData.startTime"
              type="datetime"
              placeholder="选择日期时间"
              value-format="yyyy-MM-dd HH:mm:ss"
            >
            </el-date-picker>
          </el-form-item>
          <el-form-item label="结束日期：">
            <el-date-picker
              v-model="searchData.endTime"
              type="datetime"
              placeholder="选择日期时间"
              value-format="yyyy-MM-dd HH:mm:ss"
            >
            </el-date-picker>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="onSubmit">查询</el-button>
          </el-form-item>
          <el-button type="success" @click="insertSite">球场新增</el-button>
        </el-form>
      </div>
      <el-table
        :data="tableData.list"
        style="width: 100%"
        :default-sort="{ prop: 'date', order: 'descending' }"
      >
        <el-table-column prop="id" label="序号" sortable> </el-table-column>
        <el-table-column prop="sitePlace" label="所属体育场"> </el-table-column>
        <el-table-column label="球场名称">
          <template #default="scope">
            <span v-html="scope.row.siteName"></span>
          </template>
        </el-table-column>
        <el-table-column label="球场介绍">
          <template #default="scope">
            <span v-html="scope.row.introduce.siteIntroduce"></span>
          </template>
        </el-table-column>
        <el-table-column prop="sitePersonNumber" label="踢球人数">
        </el-table-column>
        <el-table-column prop="sitePrice" label="价格"> </el-table-column>
        <el-table-column prop="siteDiscount" label="促销价格">
        </el-table-column>
        <el-table-column prop="siteTime" label="创建时间" sortable>
        </el-table-column>

        <el-table-column label="球场图片">
          <template #default="scope">
            <el-carousel indicator-position="outside" height="100px">
              <el-carousel-item
                v-for="item in scope.row.imageList.length"
                :key="item"
              >
                <el-image
                  style="width: 170px; height: 100px"
                  :src="scope.row.imageList[item - 1].img"
                ></el-image>
              </el-carousel-item>
            </el-carousel>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template #default="scope">
            <el-link type="primary" @click="updateSite(scope.row)">修改</el-link
            >&nbsp;
            <el-link type="danger" @click="deleteSiteById(scope.row.id)"
              >删除</el-link
            >
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div>
      <el-dialog :title="updateOrInsert" :visible.sync="dialogFormVisible">
        <el-form :model="echoData">
          <el-form-item label="所属体育场" :label-width="formLabelWidth">
            <el-input
              v-model="echoData.sitePlace"
              autocomplete="off"
            ></el-input>
          </el-form-item>
          <el-form-item label="球场名称" :label-width="formLabelWidth">
            <el-input v-model="echoData.siteName" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="球场介绍" :label-width="formLabelWidth">
            <vue-editor v-model="siteIntroduce"></vue-editor>
          </el-form-item>
          <el-form-item label="踢球人数" :label-width="formLabelWidth">
            <el-input-number
              v-model="echoData.sitePersonNumber"
              :min="1"
              label="描述文字"
            ></el-input-number>
          </el-form-item>
          <el-form-item label="价格" :label-width="formLabelWidth">
            <el-input-number
              v-model="echoData.sitePrice"
              :precision="2"
              :step="0.1"
            ></el-input-number>
          </el-form-item>
          <el-form-item label="促销价格" :label-width="formLabelWidth">
            <el-input-number
              v-model="echoData.siteDiscount"
              :precision="2"
              :step="0.1"
            ></el-input-number>
          </el-form-item>
          <el-form-item label="创建时间" :label-width="formLabelWidth">
            <div class="block">
              <el-date-picker
                v-model="echoData.siteTime"
                type="datetime"
                placeholder="选择日期时间"
                value-format="yyyy-MM-dd HH:mm:ss"
              >
              </el-date-picker>
            </div>
          </el-form-item>
          <el-form-item label="球场图片" :label-width="formLabelWidth">
            <el-upload
              action="http://127.0.0.1:10010/site/site/uploadFile"
              list-type="picture-card"
              :on-preview="handlePictureCardPreview"
              :on-remove="handleRemove"
              :on-success="handleAvatarSuccess"
              :file-list="imgList"
              :headers="header"
            >
              <i class="el-icon-plus"></i>
            </el-upload>
            <el-dialog :visible.sync="dialogVisible">
              <img width="100%" :src="dialogImageUrl" alt="" />
            </el-dialog>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="sureUpdate()">确 定</el-button>
        </div>
      </el-dialog>
    </div>
    <div>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage4"
        :page-sizes="[1, 3, 5]"
        :page-size="100"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>
  </div>
</template>
<script>
import { siteList, updateSite, deleteSite, insertSite } from "@/api/siteApi";
export default {
  data() {
    return {
      updateOrInsert: "",
      total: 0,
      header: {
        token: window.sessionStorage.getItem("token"),
      },
      siteIntroduce: "",
      imgList: [],
      echoData: {
        id: null,
        sitePlace: null,
        siteName: null,
        sitePersonNumber: null,
        siteNumber: null,
        sitePrice: null,
        siteDiscount: null,
        siteTime: null,
        introduce: {
          id: null,
          siteId: null,
          siteIntroduce: null,
        },
        imageList: [],
      },
      currentPage4: 1,
      dialogImageUrl: "",
      dialogVisible: false,
      dialogFormVisible: false,
      formLabelWidth: "120px",
      tableData: [],
      searchData: {
        pageNum: 1,
        pageSize: 1,
        siteName: null,
        startTime: null,
        endTime: null,
      },
    };
  },
  created() {
    this.list();
  },
  methods: {
    insertSite() {
      this.updateOrInsert = "球场新增";
      this.echoData.id = null;
      this.echoData.sitePlace = null;
      this.echoData.siteName = null;
      this.echoData.sitePersonNumber = null;
      this.echoData.siteNumber = null;
      this.echoData.sitePrice = null;
      this.echoData.siteDiscount = null;
      this.echoData.siteTime = null;
      this.echoData.introduce.id = null;
      this.echoData.introduce.siteId = null;
      this.echoData.introduce.siteIntroduce = null;
      this.echoData.imageList = [];
      this.dialogFormVisible = true;
    },
    onSubmit() {
      this.list();
    },
    handleSizeChange(val) {
      this.searchData.pageSize = val;
      this.list();
    },
    handleCurrentChange(val) {
      this.searchData.pageNum = val;
      this.list();
    },
    sureUpdate() {
      if (this.echoData.id == null) {
        this.echoData.introduce.siteIntroduce = this.siteIntroduce;
        this.echoData.imageList = [];
        for (let i = 0; i < this.imgList.length; i++) {
          this.echoData.imageList.push({ img: this.imgList[i].url });
        }
        insertSite(this.echoData).then((result) => {
          alert(result.data.data);
          this.dialogFormVisible = false;
          this.list();
        });
      } else {
        this.echoData.introduce.siteIntroduce = this.siteIntroduce;
        this.echoData.imageList = [];
        for (let i = 0; i < this.imgList.length; i++) {
          this.echoData.imageList.push({ img: this.imgList[i].url });
        }
        updateSite(this.echoData).then((result) => {
          if (result.data.flag == true) {
            alert(result.data.data);
            this.dialogFormVisible = false;
            this.list();
          } else {
            this.dialogFormVisible = false;
          }
        });
      }
    },
    handleRemove(file) {
      let index = this.imgList.indexOf(file);
      this.imgList.splice(index, 1);
    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url;
      this.dialogVisible = true;
    },
    handleAvatarSuccess(res) {
      this.imgList.push({ url: res.data });
    },
    updateSite(row) {
      this.updateOrInsert = "球场修改";
      this.echoData = row;
      this.siteIntroduce = row.introduce.siteIntroduce;
      this.imgList = [];
      for (let i = 0; i < row.imageList.length; i++) {
        this.imgList.push({ url: row.imageList[i].img });
      }
      this.dialogFormVisible = true;
    },
    deleteSiteById(id) {
      deleteSite(id).then((result) => {
        alert(result.data.data);
        this.list();
      });
    },
    list() {
      siteList(this.searchData).then((result) => {
        this.tableData = result.data.data;
        this.total = result.data.data.total;
      });
    },
  },
};
</script>
<style>
.el-carousel__item:nth-child(2n) {
  line-height: 10%;
  background-color: #99a9bf;
}

.el-carousel__item:nth-child(2n + 1) {
  background-color: #d3dce6;
}
</style>
